package iuyfgvbcd;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.coco2dxgame.R;


class DragView {
    private WindowManager windowManager;// 用于可拖动的浮动窗口
    private WindowManager.LayoutParams windowParams;// 浮动窗口的参数
    private WindowManager.LayoutParams windowParams1;// 浮动窗口的参数
    private ljkftgh thisApp;// 浮动窗口的参数
    private Button myButton;
    private Context context;
    private Boolean moveIsShow;
    private Boolean isShow;
    private static DragView ourInstance;
    private LinearLayout linearLayout;

    private int screen_width = 0;
    private int screen_height = 0;

    public static DragView getInstance() {
        if (ourInstance == null) {
            ourInstance = new DragView();
        }
        return ourInstance;
    }

    public void init(Context context1, WindowManager windowManager1, ljkftgh thisApp1) {
        moveIsShow = true;
        isShow = true;

        windowManager = windowManager1;
        context = context1;
        thisApp = thisApp1;


        windowParams = new WindowManager.LayoutParams();
        windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        windowParams1 = new WindowManager.LayoutParams();
        windowParams1.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        windowParams.format = PixelFormat.TRANSLUCENT;
        windowParams1.format = PixelFormat.TRANSLUCENT;

        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;
        screen_width = (screenWidth - 200) / 2;
        screen_height = (screenHeight - 200) / 2;

        windowParams.width = 150;
        windowParams.height = 150;
        windowParams.x = 400;
        windowParams.y = -300;


        myButton = new Button(context);
        myButton.setWidth(150);
        myButton.setHeight(150);
        myButton.getBackground().setAlpha(0);
        myButton.setScaleX(0.9f);
        myButton.setScaleY(0.9f);
        myButton.setBackgroundResource(R.mipmap.btn3);
        windowManager.addView(myButton, windowParams);

        myButton.setOnTouchListener(new View.OnTouchListener() {
            int currentX = 0;
            int currentY = 0;
            int movedX = 0;
            int movedY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        myParentButtonClick();
                        currentX = (int) (windowParams.x - event.getRawX());
                        currentY = (int) (windowParams.y - event.getRawY());
                        v.setScaleX(1f);
                        v.setScaleY(1f);
                        movedX = windowParams.x;
                        movedY = windowParams.y;
                        Log.d("zzzz", "手机按下尺寸  ");
                        Log.d("zzzz", "screen_width  " + screen_width + "  screen_height" + screen_height);
                        moveIsShow = false;
                        if (isShow == false) {
                            removeView();
                        }

                        break;
                    case MotionEvent.ACTION_MOVE:
                        int nowX = (int) event.getRawX();
                        int nowY = (int) event.getRawY();
                        windowParams.x = currentX + nowX;
                        windowParams.y = currentY + nowY;
                        if (windowParams.x < -screen_width) {
                            windowParams.x = -screen_width;
                        }
                        if (windowParams.x > screen_width) {
                            windowParams.x = screen_width;
                        }
                        if (windowParams.y < -screen_height) {
                            windowParams.y = -screen_height;
                        }
                        if (windowParams.y > screen_height) {
                            windowParams.y = screen_height;
                        }
                        windowManager.updateViewLayout(myButton, windowParams);

                        break;
                    case MotionEvent.ACTION_UP:
//                        int nowX1 = (int) event.getRawX();
//                        int nowY1 = (int) event.getRawY();
//                        movedX =  currentX + nowX1;
//                        movedY =  currentY + nowY1;
//                        windowParams.x = movedX;
//                        windowParams.y = movedY;
//                        windowManager.updateViewLayout(myButton, windowParams);

                        if (Math.abs(movedX - windowParams.x) > 10 || Math.abs(movedY - windowParams.y) > 10) {
                            moveIsShow = true;
                        }
                        Log.d("zzzz", "手机抬起尺寸  ");
                        Log.d("zzzz", "screen_width  " + screen_width + "  screen_height" + screen_height);
                        Log.d("zzzz", "moveIsShow  " + moveIsShow);
                        v.setScaleX(0.9f);
                        v.setScaleY(0.9f);
                        if (moveIsShow == false) {
                            if (isShow == true) {
                                if (windowParams.x < -screen_width) {
                                    windowParams.x = -screen_width;
                                }
                                if (windowParams.x > screen_width) {
                                    windowParams.x = screen_width;
                                }
                                if (windowParams.y < -screen_height) {
                                    windowParams.y = -screen_height;
                                }
                                if (windowParams.y > screen_height) {
                                    windowParams.y = screen_height;
                                }
                                windowManager.updateViewLayout(myButton, windowParams);
                                fangfa();
                            } else {
                                isShow = true;
                            }

                        } else {
                            isShow = true;
                        }

                        break;
                }
                return false;
            }
        });


    }

    private void fangfa() {
        windowParams1.width = 100;
        windowParams1.height = 330;
        windowParams1.x = windowParams.x;
        int posY = windowParams.y;
        if (posY > 0) {
            posY = windowParams.y - 240;
        } else {
            posY = windowParams.y + 240;
        }
        windowParams1.y = (posY);
        isShow = false;
        linearLayout = new LinearLayout(context);

        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        ));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundResource(R.mipmap.bg3);


        Button myButton1 = new Button(context);
        myButton1.setBackgroundResource(R.mipmap.home1);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                1, // 设置权重为1
                1.0f
        );
        layoutParams1.setMargins(0, 5, 0, 5);
        myButton1.setLayoutParams(layoutParams1);


        Button myButton2 = new Button(context);
        myButton2.setBackgroundResource(R.mipmap.shengying1);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                1, // 设置权重为1
                1.0f
        );
        layoutParams2.setMargins(0, 5, 0, 5);
        myButton2.setLayoutParams(layoutParams2);


        Button myButton3 = new Button(context);
        myButton3.setBackgroundResource(R.mipmap.qingchu1);
        LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                1, // 设置权重为1
                1.0f
        );
        layoutParams3.setMargins(0, 5, 0, 5);

        myButton3.setLayoutParams(layoutParams3);
        myButton1.setScaleX(0.9f);
        myButton1.setScaleY(0.9f);
        myButton2.setScaleX(0.9f);
        myButton2.setScaleY(0.9f);
        myButton3.setScaleX(0.9f);
        myButton3.setScaleY(0.9f);


        myButton1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setScaleX(1f);
                        v.setScaleY(1f);
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setScaleX(0.9f);
                        v.setScaleY(0.9f);
                        myButton1Click();
                        break;
                }
                return false;
            }
        });
        myButton2.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setScaleX(1f);
                        v.setScaleY(1f);
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setScaleX(0.9f);
                        v.setScaleY(0.9f);
                        myButton2Click();
                        break;
                }
                return false;
            }
        });
        myButton3.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setScaleX(1f);
                        v.setScaleY(1f);
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setScaleX(0.9f);
                        v.setScaleY(0.9f);
                        myButton3Click();
                        break;
                }
                return false;
            }
        });

        linearLayout.addView(myButton1);//添加iv
        linearLayout.addView(myButton2);//添加iv
        linearLayout.addView(myButton3);//添加iv

        windowManager.addView(linearLayout, windowParams1);
    }

    public void myButton1Click() {
        thisApp.myButton1Click();
        removeView();
        isShow = true;
    }

    public void myButton2Click() {
        thisApp.myButton2Click();
        removeView();
        isShow = true;
    }

    public void myButton3Click() {
        thisApp.myButton3Click();
        removeView();
        isShow = true;
    }

    public void movePos(int num) {
        if (myButton != null) {
            int width = screen_width;
            int height = screen_height;
            if (num == 0) {
                if (width > height) {
                    screen_width = height;
                    screen_height = width;
                } else {
                    screen_width = width;
                    screen_height = height;
                }
            } else {
                if (width > height) {
                    screen_width = width;
                    screen_height = height;
                } else {
                    screen_width = height;
                    screen_height = width;
                }
            }
            Log.d("zzzz", "横竖屏旋转  ");
            Log.d("zzzz", "screen_width  " + screen_width + "  screen_height" + screen_height);


            if (windowParams.x < -screen_width) {
                windowParams.x = -screen_width;
            }
            if (windowParams.x > screen_width) {
                windowParams.x = screen_width;
            }
            if (windowParams.y < -screen_height) {
                windowParams.y = -screen_height;
            }
            if (windowParams.y > screen_height) {
                windowParams.y = screen_height;
            }
            windowManager.updateViewLayout(myButton, windowParams);
        }

    }

    public void leftmovePos(int num) {
        if (myButton != null) {
            int width = screen_width;
            int height = screen_height;
            if (num == 0) {
                if (width > height) {
                    screen_width = height;
                    screen_height = width;
                } else {
                    screen_width = width;
                    screen_height = height;
                }
            } else {
                if (width > height) {
                    screen_width = width;
                    screen_height = height;
                } else {
                    screen_width = height;
                    screen_height = width;
                }
            }
            windowParams.x = -screen_width;
            windowParams.y = -screen_height;

            windowManager.updateViewLayout(myButton, windowParams);
        }

    }

    public void myParentButtonClick() {
        thisApp.myParentButtonClick();
    }

    // 删除自定义视图
    private void removeView() {
        if (linearLayout != null) {
            windowManager.removeView(linearLayout);
            linearLayout = null;
        }
    }

    public void showView() {
        if (myButton != null) {
            myButton.setVisibility(View.VISIBLE);
        }
    }

    public void hideView() {

        if (myButton != null) {
            myButton.setVisibility(View.GONE);
            if (isShow == false) {
                removeView();
                isShow = true;
            }
        }
    }

    public void removeView1() {
        if (linearLayout != null) {
            isShow = true;
            windowManager.removeView(linearLayout);
            linearLayout = null;
        }
    }


}